const db = require("../db/index"); // 数据库处理模块

// 加入购物车
exports.addGoods = (req, res) => {
  const selSql = `SELECT * FROM shopping_record WHERE id = ${req.body.userId}`;
  const addSql = `INSERT INTO shopping_record(id,addGoods) VALUES (?,?)`;
  const alterSql = `update shopping_record set addGoods = ? where id = ${req.body.userId}`;
  db.query(selSql, (err, data) => {
    if (err) return res.send(err);
    if (data.length >= 1) {
      let newarr = JSON.parse(data[0].addGoods);
      let code = false;
      newarr.forEach((item) => {
        if (item.goodsId == req.body.goodsInfo.goodsId) {
          item.goodsCount += 1;
          code = true;
        }
      });
      if (code) {
        db.query(alterSql, JSON.stringify(newarr), (err, data) => {
          if (err) return res.send(err);
          return res.send(data);
        });
      } else {
        newarr.push(req.body.goodsInfo);
        db.query(alterSql, JSON.stringify(newarr), (err, data) => {
          if (err) return res.send(err);
          res.send(data);
        });
      }
    } else {
      let arr = [req.body.goodsInfo];
      db.query(addSql, [req.body.userId, JSON.stringify(arr)], (err, data) => {
        if (err) return res.send(err);
        res.send(data);
      });
    }
  });
};

// 获取用户购物车数据列表
exports.shoppingCarList = (req, res) => {
  const sql = `SELECT * FROM shopping_record WHERE id = ${req.query.id}`;
  db.query(sql, (err, data) => {
    if (err) return res.send(err);
    if (data.length >= 1) {
      data[0].addGoods = JSON.parse(data[0].addGoods);
      res.send({ code: 200, data });
    } else {
      res.send({ code: 200, message: "购物车为空" });
    }
  });
};

// 修改商品数量
exports.alterGoodsCount = (req, res) => {
  const alterSql = `update shopping_record set addGoods = ? where id = ${req.body.userId}`;
  const selSql = `SELECT * FROM shopping_record WHERE id = ${req.body.userId}`;
  db.query(selSql, (err, data) => {
    if (err) return res.send(err);
    let newarr = JSON.parse(data[0].addGoods);
    newarr.forEach((item) => {
      if (item.goodsId == req.body.goodsData.goodsId) {
        item.goodsCount = req.body.goodsData.goodsCount;
        db.query(alterSql, JSON.stringify(newarr), (err, data) => {
          if (err) return res.send(err);
          res.send({ code: 200 });
        });
      }
    });
  });
};

// 修改商品选中状态
exports.alterGoodsChecked = (req, res) => {
  const alterSql = `update shopping_record set addGoods = ? where id = ${req.body.userId}`;
  const selSql = `SELECT * FROM shopping_record WHERE id = ${req.body.userId}`;
  db.query(selSql, (err, data) => {
    if (err) return res.send(err);

    let newarr = JSON.parse(data[0].addGoods);

    newarr.forEach((item) => {
      if (item.goodsId == req.body.goodsData.goodsId) {
        item.goodsCheck = req.body.goodsData.goodsCheck;
        db.query(alterSql, JSON.stringify(newarr), (err, data) => {
          if (err) return res.send(err);
          res.send({ code: 200, data });
        });
      }
    });
  });
};
